﻿using Framework.Common;
using Framework.Common.Paged;
using Framework.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using YouTravel.DomainService;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;
using YouTravel.QueryModels;
using YouTravel.ViewModels;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// 发票
    /// </summary>
    [RoutePrefix("api/invoice")]
    public class InvoiceController : BaseController
    {
        readonly IInvoiceService _invoiceService;
        readonly DataResult _dataResult = new DataResult();
        public InvoiceController(IInvoiceService invoiceService)
        {
            this._invoiceService = invoiceService;
        }
        /// <summary>
        /// 申请发票
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost, Route("applyinvoice"), AuthorityVerify(AuthorityRole.SpecialLineAdministrator, AuthorityRole.SpecialLineOperator)]
        public HttpResponseMessage ApplyInvoice(InvoiceViewModel invoice)
        {
            _invoiceService.ApplyInvoice(invoice, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 审核发票
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost, Route("auditinvoices"), AuthorityVerify(AuthorityRole.Branch)]
        public HttpResponseMessage AuditInvoices(InvoiceViewModel invoice)
        {
            _invoiceService.AuditInvoices(invoice, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 得到指定专线发票科目正常状态的数据
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost, Route("getinvoicesubject"), AuthorityVerify(AuthorityRole.SpecialLineAdministrator, AuthorityRole.SpecialLineOperator)]
        public HttpResponseMessage GetInvoiceSubject(InvoiceViewModel invoice)
        {
            
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _invoiceService.GetInvoiceSubject(invoice.SpecialId, LoginUser.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 得到登录人专线销售商/供应商 正常状态的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getsellersupplier"), AuthorityVerify(AuthorityRole.SpecialLineAdministrator, AuthorityRole.SpecialLineOperator)]
        public HttpResponseMessage GetSellerSupplier()
        {
            
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _invoiceService.GetSellerSupplier(LoginUser.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 得到发票详细
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost, Route("getmodel"), AuthorityVerify(AuthorityRole.Branch,AuthorityRole.SpecialLineAdministrator, AuthorityRole.SpecialLineOperator)]
        public HttpResponseMessage GetModel(InvoiceViewModel invoice)
        {
            
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _invoiceService.GetModel(invoice.ID, LoginUser.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 得到发票列表
        /// </summary>
        /// <param name="queryCond"></param>
        /// <returns></returns>
        [HttpPost, Route("getlist"), AuthorityVerify(AuthorityRole.Branch,AuthorityRole.SpecialLineAdministrator, AuthorityRole.SpecialLineOperator)]
        public HttpResponseMessage GetList(PagedParam<InvoiceQuery> queryCond)
        {
            queryCond.QueryDto.LoginUserId = LoginUser.ID;
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _invoiceService.GetList(queryCond);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
    }
}
